package com.towa.ninemanmorris;

import com.towa.ninemanmorris.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;

public class NineManMorrisActivity extends Activity {
    private NineManMorrisView nmmView;
    private DatabaseToWa db;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        db = new DatabaseToWa(this);
        
        Bundle b = getIntent().getExtras();
        
        long gameId = b.getLong("gameId", -1); 
        Log.i("NineView", "Intent " + gameId);

    	if(gameId != -1){
        	SharedPreferences settings = getSharedPreferences("NineMenMorris", MODE_PRIVATE); 
        	Editor editor = settings.edit();
        	editor.putLong("gameId", gameId);
        	editor.commit();

        }
    	Log.i("NineMenMorris","onCreate " + gameId);
        
    	if(nmmView == null){
	        nmmView = new NineManMorrisView(this);
	        setContentView(nmmView);
    	}
        Log.i("ymmot", "create");
        
    }
        
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
    	if(item.getItemId() == R.id.saveGame){
    		Log.i("NineManMorris", "Save game");
    		AlertDialog.Builder alert = new AlertDialog.Builder(this);

    		alert.setTitle("Save Game!");
    		alert.setMessage("Game Name");

    		final EditText input = new EditText(this);
    		alert.setView(input);

    		alert.setPositiveButton("Save", new DialogInterface.OnClickListener() {
    			public void onClick(DialogInterface dialog, int whichButton) {
    				Editable value = input.getText();
    				GameStatusWrapper gsw = nmmView.saveGame(value.toString()); 
    				db.saveGame(gsw);
    				dialog.dismiss();
    			}
    		});

    		alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    			public void onClick(DialogInterface dialog, int whichButton) {
    				dialog.dismiss();
    			}
    		});

    		alert.show();
    		return true;
    	}
    	return false;
    }

    public void returnToMenu() {
    	Intent intent = new Intent(NineManMorrisActivity.this, WelcomeActivity.class);
    	setResult(Activity.RESULT_OK, intent);
    	finish();
    }
    
    @Override
    public void onResume(){
    	super.onResume();
    	SharedPreferences settings = getSharedPreferences("NineMenMorris", MODE_PRIVATE);
    	long gameId = settings.getLong("gameId", -1);
//    	Log.i("NineMenMorris","Resume GameId: " + gameId);
    	GameStatusWrapper gsw = new GameStatusWrapper();
//    	Log.i("NineView", "onResumeGameId: " + gameId);
    	if(gameId != -1){
    		gsw = db.loadGame(gameId);
        }
//    	Log.i("NineMenMorris", "loadStart");
//    	Log.i("NineMenMorrisStatus", "onResume " + gsw.toString());
    	Log.i("ymmot", "onResume1: " + gsw.getRedsOnBoard());
    	nmmView.loadGame(gsw);
    	Log.i("ymmot", "onResume2: " + gsw.getRedsOnBoard());
//    	Log.i("NineMenMorris", "loadDone");
    }
    
    @Override
    public void onPause(){
    	super.onPause();
//    	Log.i("NineMenMorris", "onPause");
    	GameStatusWrapper gsw = nmmView.saveGame("Current");
//    	long id = gsw.getId(); 
//    	if(id < 0)
//    		id = db.saveGame(gsw);
//    	else{
//    		Log.i("NineMenMorris" , "Update DB!!");
    	Log.i("NineMenMorrisStatus", "onPause " + gsw.toString());
    	Log.i("ymmot", "onPause1: " + gsw.getRedsOnBoard());
    		db.updateGame(gsw);	
    		Log.i("ymmot", "onPause2: " + gsw.getRedsOnBoard());
//    	Log.i("NineMenMorris", "updateDone");
//    	}
    	SharedPreferences settings = getSharedPreferences("NineMenMorris", MODE_PRIVATE); 
    	Editor editor = settings.edit();
    	editor.putLong("gameId", 1);
    	editor.commit();
//    	Log.i("NineMenMorris","onPuase done " + id);
    }
    
    @Override
	public void onDestroy() {
		super.onDestroy();
		db.close();
	}
    
}